package com.qymerge.manage.base.service.impl;

import com.qymerge.manage.base.dao.IMetaOrgDao;
import com.qymerge.manage.base.dao.IMetaOrgVersionDao;
import com.qymerge.manage.base.ro.MetaOrgRO;
import com.qymerge.manage.base.ro.OrganizationalStructureRO;
import com.qymerge.manage.base.service.IMetaOrgService;
import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.bean.DataTableRO;
import com.qymerge.manage.core.bean.PageRequest;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>Title: MetaOrgServiceImpl</p>
 * <p>Description: 组织机构</p>
 *
 * @author Xiao
 * @date 2025/4/29 18:43
 * @version 1.0.0
 */
@Service
public class MetaOrgServiceImpl implements IMetaOrgService {

    @Autowired
    private IMetaOrgDao metaOrgDao;
    @Autowired
    private IMetaOrgVersionDao metaOrgVersionDao;

    /**
     * 查询
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public DataTableRO list(MetaOrgRO ro, PageRequest pageRequest) {
        List<MetaOrgRO> list = metaOrgDao.list(ro, pageRequest);
        for (MetaOrgRO metaOrgRO : list) {
            metaOrgRO.setOrgVersionInfo(metaOrgVersionDao.findOne(metaOrgRO.getOrgVersionId()));
        }
        DataTableRO dataTableRO = new DataTableRO();
        int allCount = metaOrgDao.total(ro);
        int pageSize = pageRequest.getSize();
        dataTableRO.setAll_count(allCount);
        dataTableRO.setList(list);
        dataTableRO.setCurrent_page(pageRequest.getPage());
        dataTableRO.setPage_size(pageSize);
        int allPage = (allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1);
        dataTableRO.setAll_page(allPage);
        return dataTableRO;
    }

    @Override
    public List<MetaOrgRO> list(MetaOrgRO ro) {
        return metaOrgDao.findAll(ro);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public MetaOrgRO detail(Long id) {
        return metaOrgDao.findOne(id);
    }

    @Override
    public ActionResultRO save(MetaOrgRO ro) {
        int rn = metaOrgDao.insert(ro);
        return new ActionResultRO(rn, "保存成功", "保存失败");
    }

    @Override
    public ActionResultRO edit(MetaOrgRO ro) {
        int rn = metaOrgDao.update(ro);
        return new ActionResultRO(rn, "修改成功", "修改失败");
    }

    @Override
    public ActionResultRO delete(Long id) {
        String msg = "";
        String errMsg = "";
        int rn = 0;

        // 二级查询
        List<MetaOrgRO> secondAll = metaOrgDao.findAllByParentId(id);
        if (CollectionUtils.isNotEmpty(secondAll)) {
            for (MetaOrgRO second : secondAll) {
                long secondParentId = second.getId();

                // 三级
                List<MetaOrgRO> thirdAll = metaOrgDao.findAllByParentId(secondParentId);
                if (CollectionUtils.isNotEmpty(thirdAll)) {
                    for (MetaOrgRO third : thirdAll) {
                        Long thirdParent = third.getId();

                        // 四级
                        List<MetaOrgRO> fourAll = metaOrgDao.findAllByParentId(thirdParent);
                        if (CollectionUtils.isNotEmpty(fourAll)) {
                            for (MetaOrgRO four : fourAll) {
                                long fourParentId = four.getId();

                                // 五级
                                List<MetaOrgRO> fiveAll = metaOrgDao.findAllByParentId(fourParentId);
                                if (CollectionUtils.isNotEmpty(fiveAll)) {
                                    for (MetaOrgRO five : fiveAll) {
                                        Long fiveParentId = five.getId();

                                        // 六级
                                        List<MetaOrgRO> sixAll = metaOrgDao.findAllByParentId(fiveParentId);
                                        if (CollectionUtils.isNotEmpty(fiveAll)) {
                                            for (MetaOrgRO six : sixAll) {
                                                Long sixParentId = six.getId();
                                                metaOrgDao.remove(sixParentId);
                                            }
                                        }
                                        metaOrgDao.remove(fiveParentId);
                                    }
                                }
                                metaOrgDao.remove(fourParentId);
                            }
                        }
                        metaOrgDao.remove(thirdParent);
                    }
                }
                metaOrgDao.remove(secondParentId);
            }
        }
        // 删除本菜单
        rn = metaOrgDao.remove(id);
        msg = "删除成功";
        errMsg = "删除失败";

        return new ActionResultRO(rn, msg, errMsg);
    }


}
